// pages/mine/repair/repairDetail/repairDetail.js
const app = getApp()
import Dialog from '../../../../miniprogram_npm/_vant-weapp@0.5.18@vant-weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:null, // 1: 未处理 2：已处理 3：已完成
    repairId:null,
    receiveShow:false,
    remark:"无",
    repairImg:[],
    title:"",
    room_number:"",
    createTime:"",
    headImg:"",
    tenant_name:"",
    tenant_tel:"",
    records:[],

    repairShow:false,//维修记录弹框显示控制器
    image: [],//添加图片
    imageLength:0,
    note: null,//备注
    noteLength: 0,
    showInput: false,
    addRecordTitle:"添加维修记录",

    isEdit: false,//维修记录弹框确定事件控制器
    editId: null,//维修记录ID

    disabled:false,//领取任务按钮是否禁用

    prepage:null,//父页面传的page
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({ 
      type:options.type || null,
      repairId:options.id || null,
      prepage:options.page || null,
    })
    this.showDetail()
  },

  onShow: function(){
    
  },
  /**
   * 生命周期函数--监听页面渲染完成
   */
  onReady: function () {
    wx.nextTick(() => {
      if (this.data.records.length > 0) {
        this.setData({
          addRecordTitle: "继续添加维修记录"
        })
      }else{
        this.setData({
          addRecordTitle: '添加维修记录'
        })
      }
    })
  },
  /**
   * 上传图片和删除 同一个方法
   */
  uploadImage(res) {
    this.data.image = res.detail.fileList.map((item, index) => {
      console.log(index)
      return item.url
    })
    this.setData({
      imageLength: res.detail.fileList.length
    })
  },
  /**
   * textarea点击
   */
  changeTap(e) {
    this.setData({
      showInput: e.currentTarget.dataset.type
    })
  },
  /**
   * textarea输入
   */
  changeInput(e) {
    this.setData({
      note: e.detail.value,
      noteLength: e.detail.value.length
    })
    if (this.data.noteLength > 100) {
      wx.showToast({
        title: '备注限制输入100个字符',
        icon: "none"
      })
    }
  },
  /**
   * textarea失去焦点
   */
  changeBlur(e) {
    this.setData({
      showInput: e.currentTarget.dataset.type
    })
  },
  /**
   * 页面显示
   */
  showDetail(){
    wx.showLoading({
      title: '正在加载',
    })
    app.globalData.request("get","applet/repair/details",{
      repair_id: this.data.repairId
    })
    .then(res => {
      this.setData({
        headImg: res.data.icon,
        title: res.data.title,
        room_number: res.data.room_number,
        createTime: res.data.create_time,
        repairImg: res.data.image,
        remark: res.data.remark,
        tenant_name: res.data.tenant_name,
        tenant_tel: res.data.tenant_tel,
        records: res.data.records
      })
      if (res.data.records.length == 0) {
        this.setData({ addRecordTitle: "添加维修记录" })
      }
      wx.nextTick(() => {
        wx.hideLoading()
      })
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: err.msg,
        icon: "none"
      })
    })
  },
  /**
   * 领取任务
   * 
   */
  receive(){
    this.setData({
      receiveShow: true
    })
  },
  // 领取任务 --确定按钮
  taskConfirm(e) {
    app.globalData.request("get", "applet/repair/receiveTasks", {
      id: wx.getStorageSync('id'),
      repair_id: this.data.repairId,
      p:this.data.prepage
    })
      .then(res => {
        wx.showToast({
          title: '领取成功',
        })
        this.setData({
          receiveShow: false,
          disabled: true
        })
        let pagetotal = getCurrentPages() //所有的路径 数组
        let thispage = pagetotal[pagetotal.length - 1] //当前页面
        let parpage = pagetotal[pagetotal.length - 2] //父级页面
        if(res.data != null){
          parpage.setData({
            arr: res.data,
            arrpage: this.data.prepage,
            arrid: this.data.repairId
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: "none"
        })
      })
  },
  // 领取任务 --取消
  taskCancel() {
    this.setData({
      receiveShow: false
    })
  },
  // 联系租客
  contact(){
    wx.makePhoneCall({
      phoneNumber: this.data.tenant_tel,
    })
  },
  // 关闭任务
  closeTask(){
    Dialog.confirm({
      title: '标题',
      message: '确认关闭这条报修任务吗？',
      asyncClose: true
    })
    .then(() => {
      app.globalData.request("get", "applet/repair/maintenanceCompleted", {
        repair_id: this.data.repairId
      })
      .then(res => {
        Dialog.close();

        let pagetotal = getCurrentPages() //所有的路径 数组
        let thispage = pagetotal[pagetotal.length - 1] //当前页面
        let parpage = pagetotal[pagetotal.length - 2] //父级页面
        //父级页面需要有一个arr的数组定义
        parpage.setData({
          arrstr: "needToRefresh"
        })

        wx.navigateBack({
          delta:1
        })
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: "none"
        })
      })
    })
    .catch(() => {
      Dialog.close();
    });
  },
  // 添加维修记录
  addRecords(e){
    this.setData({
      image: [],
      imageLength: 0,
      note: null,
      noteLength: 0,
      repairShow:true,
      isEdit:e.currentTarget.dataset.edit
    })
  },
  // 关闭弹框--维修记录
  closeCheck(){
    this.setData({ 
      repairShow: false,
    })
  },
  // 维修记录 -- 弹框确定
  checkItem(){
    if(this.data.noteLength > 100){
      wx.showToast({
        title: '备注限制输入100个字符',
        icon: "none"
      })
    }else{
      // isedit：true为编辑事件  false为添加事件
      let url = null, datas = null
      if (this.data.isEdit) {
        url = "applet/repair/recordEditor"
        datas = {
          m_id: this.data.editId,
          image: this.data.image,
          remark: this.data.note
        }
      } else {
        url = "applet/repair/maintenanceRecord",
          datas = {
            u_id: wx.getStorageSync("id"),
            repair_id: this.data.repairId,
            image: this.data.image,
            remark: this.data.note
          }
      }
      app.globalData.request("post", url, datas)
        .then(res => {
          wx.showToast({
            title: res.msg,
          })
          this.showDetail()
          this.setData({
            addRecordTitle: "继续添加维修记录",
            repairShow: false
          })
        }).catch(err => {
          wx.showToast({
            title: err.msg,
            icon: "none"
          })
        })
    }
  },
  // 编辑记录
  editRecord(e){
    this.setData({
      repairShow: true,
      isEdit: e.currentTarget.dataset.edit,
      editId: e.currentTarget.dataset.rid,
    })
    app.globalData.request("get","applet/repair/recordEditor",{
      m_id:e.currentTarget.dataset.rid
    })
    .then(res => {
      this.setData({
        image: res.data.m_image,
        imageLength: res.data.m_image.length,
        note: res.data.m_remark,
        noteLength: res.data.m_remark.length
      })
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: "none"
      })
    })
  },
  // 删除记录
  deleteRecord(e){
    Dialog.confirm({
      title: '标题',
      message: '确认删除这条维修记录吗？',
      asyncClose: true
    })
    .then(() => {
      app.globalData.request("get", "applet/repair/recordDel", {
        m_id: e.currentTarget.dataset.rid
      })
      .then(res => {
        Dialog.close();
        this.showDetail()
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: "none"
        })
      })
    })
    .catch(() => {
      Dialog.close();
    });
  },
})